﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;

namespace Kgb.Model.Models
{
    public class Player
    {
        private string session;
        public int Id {get;set;}
        public string SessionId
        {
            get
            {
                if (Id == 0)
                    return session;
                else
                    return "";
            }
            set
            {
                session = value;
            } 
        }

        [Required]
        [StringLength(20, ErrorMessage = "O {0} deve ter entre 2 e 20 caracteres.", MinimumLength = 2)]
        [DataType(DataType.Text)]
        [Display(Name = "Nickname")]
        public string Name {get;set;}
        public string State { get; set; } //waiting, playing
        public int? LobbyId { get; set; }
        public int? RoomId { get; set; }

        public Player()
        {
            LobbyId = null;
            RoomId = null;
            SessionId = Guid.NewGuid().ToString();
        }

        public Player(string name, string sessionId)
        {
            try
            {
                if(!string.IsNullOrEmpty(sessionId))
                    Guid.Parse(sessionId);
            }
            catch (Exception ex)
            {
                throw new Exception("Não foi possível criar o jogador! Id de sessão inválida.", ex);
            }

            LobbyId = null;
            RoomId = null;
            Name = name;
            SessionId = sessionId;
        }

        public bool ValidateSession(string sessionId)
        {
            return session.Equals(sessionId);
        }

        public string Session()
        {
            return session;
        }
    }
}